package com.ruoyi.web.controller.app;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.alibaba.excel.util.StringUtils;
import com.ruoyi.app.domain.bo.AppPackageBo;
import com.ruoyi.app.domain.vo.AppPackageVo;
import com.ruoyi.app.domain.vo.AppUserVo;
import com.ruoyi.app.service.IAppPackageOrderService;
import com.ruoyi.app.service.IAppPackageService;
import com.ruoyi.app.service.IAppUserService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * APP套餐
 *
 * @author ruoyi
 * @date 2025-06-21
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/package")
public class AppPackageController extends BaseController {

    private final IAppUserService appUserService;
    private final IAppPackageService iAppPackageService;
    private final IAppPackageOrderService appPackageOrderService;


    /**
     * 查询APP套餐列表
     */
    @SaCheckPermission("system:package:list")
    @GetMapping("/list")
    public TableDataInfo<AppPackageVo> list(AppPackageBo bo, PageQuery pageQuery) {
        return iAppPackageService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出APP套餐列表
     */
    @SaCheckPermission("system:package:export")
    @Log(title = "APP套餐", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(AppPackageBo bo, HttpServletResponse response) {
        List<AppPackageVo> list = iAppPackageService.queryList(bo);
        ExcelUtil.exportExcel(list, "APP套餐", AppPackageVo.class, response);
    }

    /**
     * 获取APP套餐详细信息
     *
     * @param packageId 主键
     */
    @SaCheckPermission("system:package:query")
    @GetMapping("/{packageId}")
    public R<AppPackageVo> getInfo(@NotNull(message = "主键不能为空")
                                   @PathVariable Long packageId) {
        return R.ok(iAppPackageService.queryById(packageId));
    }

    /**
     * 新增APP套餐
     */
    @SaCheckPermission("system:package:add")
    @Log(title = "APP套餐", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody AppPackageBo bo) {
        return toAjax(iAppPackageService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改APP套餐
     */
    @SaCheckPermission("system:package:edit")
    @Log(title = "APP套餐", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody AppPackageBo bo) {
        return toAjax(iAppPackageService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 赠送用户套餐
     */
    @SaCheckPermission("system:package:edit")
    @Log(title = "APP套餐", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/send")
    public R<Void> sendEdit(@RequestBody AppPackageBo bo) {
        if (bo.getPackageId() == null) {
            return R.fail("请选择赠送任务包");
        }
        AppPackageVo packageVo = iAppPackageService.queryById(bo.getPackageId());
        if (Objects.isNull(packageVo)) {
            return R.fail("任务包不存在");
        }
        if (StringUtils.isBlank(bo.getUserName())) {
            return R.fail("请输入赠送用户手机号");
        }
        AppUserVo userVo = appUserService.queryByPhone(bo.getUserName());
        if (Objects.isNull(userVo)) {
            return R.fail("赠送手机号不存在");
        }
        return toAjax(appPackageOrderService.sendTask(packageVo, userVo.getId()));
    }

    /**
     * 删除APP套餐
     *
     * @param packageIds 主键串
     */
    @SaCheckPermission("system:package:remove")
    @Log(title = "APP套餐", businessType = BusinessType.DELETE)
    @DeleteMapping("/{packageIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] packageIds) {
        return toAjax(iAppPackageService.deleteWithValidByIds(Arrays.asList(packageIds), true) ? 1 : 0);
    }
}
